package tako.wisers.com.android.jdo;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import tako.wisers.com.android.upload.EntityBase;

import com.google.appengine.api.datastore.Text;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class StationJdo extends EntityBase {
	private static final long serialVersionUID = -3837643539069515384L;

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;

	@Persistent
	private Text name;
	
	@Persistent
	private long nameHash;

	@Persistent
	private Text address;

	@Persistent
	private Text city;

	@Persistent
	private Text countyCode;

	@Persistent
	private Text phone;

	@Persistent
	private int type;

	@Persistent
	private int subType;
	
	@Persistent
	private int grade;

	@Persistent
	private double lat;

	@Persistent
	private double lon;

	@Persistent
	private String cellCode;

	@Persistent
	private Text url;

	@Persistent
	private Text photoURL;

	@Persistent
	private Text info;

	@Persistent
	private int times;
	
	@Persistent
	private int shop;
	
	@Persistent
	private int location;

	public StationJdo() {
		super();
	}

	public StationJdo(String name, String address, String city,
			String countyCode, String phone, int type, double lat, double lon,
			String cellCode, String url, String info, String photoURL, int shop, int location) {
		super();
		this.name = new Text(name);
		this.nameHash = getHashcode(name);
		this.address = new Text(address);
		this.city = new Text(city);
		this.countyCode = new Text(countyCode);
		this.phone = new Text(phone);
		this.type = type;
		this.lat = lat;
		this.lon = lon;
		this.cellCode = cellCode;
		this.url = new Text(url);
		this.info = new Text(info);
		this.photoURL = new Text(photoURL);
		this.shop = shop;
		this.location = location;
	}

	public int getTimes() {
		return times;
	}

	public int getShop() {
		return shop;
	}

	public void setShop(int shop) {
		this.shop = shop;
	}

	public int getLocation() {
		return location;
	}

	public void setLocation(int location) {
		this.location = location;
	}

	public Text getName() {
		return name;
	}

	public Text getPhotoURL() {
		return photoURL;
	}

	public void setPhotoURL(Text photoURL) {
		this.photoURL = photoURL;
	}

	public void setName(Text name) {
		this.name = name;
	}

	public int getSubType() {
		return subType;
	}

	public void setSubType(int subType) {
		this.subType = subType;
	}

	public Text getInfo() {
		return info;
	}

	public void setInfo(Text info) {
		this.info = info;
	}

	public Text getAddress() {
		return address;
	}

	public void setAddress(Text address) {
		this.address = address;
	}

	public Text getCity() {
		return city;
	}

	public void setCity(Text city) {
		this.city = city;
	}

	public Text getCountyCode() {
		return countyCode;
	}

	public void setCountyCode(Text countyCode) {
		this.countyCode = countyCode;
	}

	public Text getPhone() {
		return phone;
	}

	public void setPhone(Text phone) {
		this.phone = phone;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public double getLat() {
		return lat;
	}

	public void setLat(double lat) {
		this.lat = lat;
	}

	public double getLon() {
		return lon;
	}

	public void setLon(double lon) {
		this.lon = lon;
	}

	public String getCellCode() {
		return cellCode;
	}

	public void setCellCode(String cellCode) {
		this.cellCode = cellCode;
	}

	public Text getUrl() {
		return url;
	}

	public void setUrl(Text url) {
		this.url = url;
	}

	@Override
	public void insert(PersistenceManager pm, String line, Integer times) {
		String[] attrs = line.split(",");
		this.name = new Text(attrs[0]);
		this.nameHash = getHashcode(attrs[0]);
		this.address = new Text(attrs[1]);
		this.city = new Text(attrs[2]);
		this.countyCode = new Text(attrs[3]);
		this.phone = new Text(attrs[4]);
		this.type = new Integer(attrs[5]);
		if (!attrs[6].equals("")) {
			this.subType = new Integer(attrs[6]);
		}
		this.shop = new Integer(attrs[7]);
		this.location = new Integer(attrs[8]);
		this.lat = new Double(attrs[9]);
		this.lon = new Double(attrs[10]);
		this.cellCode = new Geohash().encode(lat, lon);
		this.info = new Text(attrs[11]);
		this.url = new Text(attrs[12]);
		if(attrs.length == 14){
			this.photoURL = new Text(attrs[13]);
		}

		pm.makePersistent(this);
		pm.flush();
	}

	@Override
	public void update(PersistenceManager pm) {

	}

	@Override
	public String toString() {
		return name.getValue() + "," + address.getValue() + ","
				+ city.getValue() + "," + countyCode.getValue() + ","
				+ phone.getValue() + "," + type + "," + subType + "," + lat
				+ "," + lon + "," + info.getValue() + "," + url.getValue()
				+ "," + photoURL.getValue();
	}
	
	public static synchronized long getHashcode(String query) {
		String reverse = (new StringBuffer(query)).reverse().toString();
		long hashcode = reverse.hashCode();
		hashcode = hashcode << 32;
		hashcode = hashcode + query.hashCode();
		return hashcode;
	}

	public int getGrade() {
		return grade;
	}

	public void setGrade(int grade) {
		this.grade = grade;
	}

	public long getNameHash() {
		return nameHash;
	}
	
	
	
}
